package com.ydgk.service.impl;

import com.ydgk.bean.Logistics;
import com.ydgk.bean.Message;
import com.ydgk.bean.Order;
import com.ydgk.bean.Pre_order;
import com.ydgk.dao.LogisticsMapper;
import com.ydgk.dao.MessageMapper;
import com.ydgk.dao.OrderMapper;
import com.ydgk.dao.Pre_orderMapper;
import com.ydgk.service.MyOrdersService;
import com.ydgk.tool.Tool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

@Service
public class MyOrdersServiceImpl implements MyOrdersService {
    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private Pre_orderMapper pre_orderMapper;

    @Autowired
    private LogisticsMapper logisticsMapper;

    @Autowired
    private MessageMapper messageMapper;

    public int findAllOrderCount(Map<String, Object> factorMap) {
        return orderMapper.findAllCount(factorMap);
    }

    public List<Order> findAllOrder(Map<String, Object> factorMap) {
        return orderMapper.findAll(factorMap);
    }

    public Order findOrderById(Integer oid) {
        return orderMapper.findById(oid);
    }

    public void deleteOrder(Integer oid) {
        pre_orderMapper.deleteByOid(oid);
        Logistics logistics = logisticsMapper.findByOid(oid);
        if(logistics != null){
            messageMapper.deleteByLid(logistics.getLid());
            logisticsMapper.deleteByOid(oid);
        }
        orderMapper.deleteById(oid);
    }

    public void sendOrder(Order order) {
        Logistics logistics = logisticsMapper.findByOid(order.getOid());
        String lnum = Tool.getOrderNumber();
        logistics.setLnum(lnum);
        logistics.setLtime(new Timestamp(System.currentTimeMillis()));
        logisticsMapper.updateById(logistics);
        String msg = "商家已发货";
        Message message = new Message(null,new Timestamp(System.currentTimeMillis()),msg,logistics.getLid());
        messageMapper.insert(message);
        order.setOtype(1);
        orderMapper.updateById(order);
    }

    public List<Order> findAllSendOrder() {
        return orderMapper.findAllSendOrder();
    }
}
